package com.htthorizon.system.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.htthorizon.common.core.constant.Constants;
import com.htthorizon.common.core.domain.R;
import com.htthorizon.common.core.exception.LogCapcityOverException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.htthorizon.common.core.utils.poi.ExcelUtil;
import com.htthorizon.common.core.web.controller.BaseController;
import com.htthorizon.common.core.web.domain.AjaxResult;
import com.htthorizon.common.core.web.page.TableDataInfo;
import com.htthorizon.common.log.annotation.Log;
import com.htthorizon.common.log.enums.BusinessType;
import com.htthorizon.common.security.annotation.InnerAuth;
import com.htthorizon.common.security.annotation.RequiresPermissions;
import com.htthorizon.system.api.domain.SysOperLog;
import com.htthorizon.system.service.ISysOperLogService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 操作日志记录
 * 
 * @author mark
 */
@RestController
@RequestMapping("/operlog")
public class SysOperlogController extends BaseController
{
    @Autowired
    private ISysOperLogService operLogService;

    @RequiresPermissions("system:operlog:list")
    @GetMapping("/list")
    @Log(title = "系统管理/操作日志", businessType = BusinessType.QUERY,type = Constants.SYSTEM)
    public TableDataInfo list(SysOperLog operLog)
    {
        startPage();
        List<SysOperLog> list = operLogService.selectOperLogList(operLog);
        return getDataTable(list);
    }

    @GetMapping("/count")
    public R count(){
       int count= operLogService.count();
       return R.ok(count);
    }

    @Log(title = "系统管理/操作日志", businessType = BusinessType.EXPORT,type = Constants.SYSTEM)
    @RequiresPermissions("system:operlog:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOperLog operLog)
    {
        List<SysOperLog> list = operLogService.selectOperLogList(operLog);
        ExcelUtil<SysOperLog> util = new ExcelUtil<SysOperLog>(SysOperLog.class);
        util.exportExcel(response, list, "操作日志");
    }

    @PostMapping("/import")
    public R importExcel(MultipartFile file) throws IOException {
        ExcelUtil<SysOperLog> util = new ExcelUtil<SysOperLog>(SysOperLog.class);
        List<SysOperLog> sysOperLogs = util.importExcel(file.getInputStream());
          operLogService.insertOperLogs(sysOperLogs);
        return R.ok();
    }

    @Log(title = "操作日志", businessType = BusinessType.DELETE,type = Constants.SYSTEM)
    @RequiresPermissions("system:operlog:remove")
    @GetMapping("/del/{operIds}")
    public AjaxResult remove(@PathVariable Long[] operIds)
    {
        return toAjax(operLogService.deleteOperLogByIds(operIds));
    }
//
//    @RequiresPermissions("system:operlog:remove")
//    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
//    @DeleteMapping("/clean")
//    public AjaxResult clean()
//    {
//        operLogService.cleanOperLog();
//        return success();
//    }

    @InnerAuth
    @PostMapping
    public AjaxResult add(@RequestBody SysOperLog operLog)
    {
        return toAjax(operLogService.insertOperlog(operLog));
    }

    @GetMapping("/getLogCapcity")
    public R<Boolean> getLogCapcity(){
        if (operLogService.getLogCapcity()) {
            return R.ok();
        }
        return R.fail("日志超出容量，请联系管理员");

    }

}
